<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/Frog_32px_1177822_easyicon.net.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/Frog_32px_1177822_easyicon.net.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/Frog_16px_1177822_easyicon.net.ico">
  <link rel="mask-icon" href="/images/Frog_32px_1177822_easyicon.net.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"hxy1997.xyz","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":true,"nav":null,"activeClass":"valine"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"输入关键字","hits_empty":"没有找到与「${query}」相关搜索","hits_stats":"${hits} 条相关记录，共耗时 ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.json"};
  </script>

  <meta name="description" content="TypeScript 的类型校验是给程序员看的，在编译后不会存在 TS 代码。">
<meta property="og:type" content="article">
<meta property="og:title" content="TypeScript笔记">
<meta property="og:url" content="https://hxy1997.xyz/2021/05/07/TypeScript%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="hxy的博客">
<meta property="og:description" content="TypeScript 的类型校验是给程序员看的，在编译后不会存在 TS 代码。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-05-07T04:30:00.000Z">
<meta property="article:modified_time" content="2021-05-14T09:06:43.348Z">
<meta property="article:author" content="hxy">
<meta property="article:tag" content="JavaScript">
<meta property="article:tag" content="编程语言">
<meta property="article:tag" content="Typescript">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://hxy1997.xyz/2021/05/07/TypeScript%E7%AC%94%E8%AE%B0/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>TypeScript笔记 | hxy的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="hxy的博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">hxy的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Mia san Mia!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/huxingyi1997" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://hxy1997.xyz/2021/05/07/TypeScript%E7%AC%94%E8%AE%B0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/Robben.gif">
      <meta itemprop="name" content="hxy">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="hxy的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          TypeScript笔记
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-05-07 12:30:00" itemprop="dateCreated datePublished" datetime="2021-05-07T12:30:00+08:00">2021-05-07</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-05-14 17:06:43" itemprop="dateModified" datetime="2021-05-14T17:06:43+08:00">2021-05-14</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/web%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">web前端</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="热度" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">热度：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/05/07/TypeScript%E7%AC%94%E8%AE%B0/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/05/07/TypeScript%E7%AC%94%E8%AE%B0/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>TypeScript 的类型校验是给程序员看的，在编译后不会存在 TS 代码。</p>
<span id="more"></span>

<h2 id="1-类型注解"><a href="#1-类型注解" class="headerlink" title="1.类型注解"></a>1.类型注解</h2><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params">person: <span class="built_in">string</span></span>): <span class="title">void</span> </span>&#123;</span><br><span class="line">  <span class="comment">// 参数类型是字符串，没有返回值</span></span><br><span class="line">  <span class="comment">//...</span></span><br><span class="line">&#125;</span><br><span class="line">fn(<span class="string">&quot;str&quot;</span>); <span class="comment">// 如传递参数非字符串，vscode编辑器中或在编译时将给出错误提示</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> test: <span class="built_in">number</span> = <span class="number">1</span>;</span><br></pre></td></tr></table></figure>

<h3 id="1-1-有哪些基础类型注解？"><a href="#1-1-有哪些基础类型注解？" class="headerlink" title="1.1 有哪些基础类型注解？"></a>1.1 有哪些基础类型注解？</h3><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 基础类型</span></span><br><span class="line">:<span class="built_in">string</span></span><br><span class="line">:<span class="built_in">number</span></span><br><span class="line">:<span class="built_in">boolean</span></span><br><span class="line"></span><br><span class="line">:<span class="literal">null</span>  <span class="comment">// 只能是null值</span></span><br><span class="line">:<span class="literal">undefined</span> <span class="comment">// 只能是undefined值</span></span><br><span class="line"></span><br><span class="line">:symbol</span><br><span class="line"></span><br><span class="line"><span class="comment">// 引用类型</span></span><br><span class="line">:<span class="built_in">object</span> <span class="comment">// 不常用，多使用接口来给对象进行类型注解</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 其他</span></span><br><span class="line">:<span class="built_in">any</span>  <span class="comment">// 任意类型</span></span><br><span class="line">:<span class="built_in">void</span>  <span class="comment">// 空，用于函数的无返回值的注解</span></span><br><span class="line">:<span class="built_in">never</span> <span class="comment">// 用于标注函数代码永远执行不完（如抛出错误的函数，死循环函数）</span></span><br></pre></td></tr></table></figure>

<h3 id="1-2-什么是类型注解和类型推断、类型断言？"><a href="#1-2-什么是类型注解和类型推断、类型断言？" class="headerlink" title="1.2 什么是类型注解和类型推断、类型断言？"></a>1.2 什么是类型注解和类型推断、类型断言？</h3><p><strong>类型注解</strong> 是显式的标注类型</p>
<p><strong>类型推断</strong> 是编辑器根据值来自动推断出类型 (编辑器中鼠标移到变量会显示类型的提示)。</p>
<p><strong>类型断言</strong> 是告诉编译器，“相信我，它就是这个类型”。</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 类型注解就是显式的写出类型</span></span><br><span class="line"><span class="keyword">const</span> myNumber: <span class="built_in">number</span> = <span class="number">123</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 类型推断是根据值来推断类型 (鼠标移到变量会显示类型(或直接显示值)的提示)</span></span><br><span class="line"><span class="keyword">const</span> myString = <span class="string">&quot;test&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 类型断言（开发者明确知道是什么类型）</span></span><br><span class="line"><span class="keyword">const</span> someValue: <span class="built_in">any</span> = <span class="string">&quot;abc&quot;</span>;</span><br><span class="line"><span class="keyword">const</span> strLength: <span class="built_in">number</span> = (someValue <span class="keyword">as</span> <span class="built_in">string</span>).length; <span class="comment">// 断言someValue是字符串</span></span><br></pre></td></tr></table></figure>

<h3 id="1-3-对象的类型"><a href="#1-3-对象的类型" class="headerlink" title="1.3 对象的类型"></a>1.3 对象的类型</h3><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 对象字面量的类型检查</span></span><br><span class="line"><span class="keyword">const</span> xiaojiejie: &#123;</span><br><span class="line">  name: <span class="built_in">string</span>;</span><br><span class="line">  age: <span class="built_in">number</span>;</span><br><span class="line">&#125; = &#123;</span><br><span class="line">  name: <span class="string">&quot;小红&quot;</span>,</span><br><span class="line">  age: <span class="number">18</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 标注:object</span></span><br><span class="line"><span class="keyword">const</span> obj: <span class="built_in">object</span> = &#123;&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 使用接口</span></span><br><span class="line"><span class="keyword">interface</span> Person &#123;</span><br><span class="line">  name: <span class="built_in">string</span>;</span><br><span class="line">  age: <span class="built_in">number</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> xjj: Person = &#123;</span><br><span class="line">  name: <span class="string">&quot;xh&quot;</span>,</span><br><span class="line">  age: <span class="number">18</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// class类的类型检查</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Parson</span> </span>&#123;&#125;</span><br><span class="line"><span class="keyword">const</span> xiaobai: Parson = <span class="keyword">new</span> Parson();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 函数和返回值的类型检查</span></span><br><span class="line"><span class="keyword">const</span> fn: <span class="function">() =&gt;</span> <span class="built_in">string</span> = <span class="function">() =&gt;</span> <span class="string">&quot;123&quot;</span>;</span><br></pre></td></tr></table></figure>

<h3 id="1-4-函数的类型注解"><a href="#1-4-函数的类型注解" class="headerlink" title="1.4 函数的类型注解"></a>1.4 函数的类型注解</h3><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 函数返回值的类型注解 fn(): number &#123;&#125;</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getTotal2</span>(<span class="params">one: <span class="built_in">number</span>, two: <span class="built_in">number</span></span>): <span class="title">number</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> one + two;</span><br><span class="line">&#125;</span><br><span class="line">getTotal2(<span class="number">1</span>, <span class="number">2</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 无返回值: void</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sayHello</span>(<span class="params"></span>): <span class="title">void</span> </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;Hello&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 永远执行不玩的函数</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">setTimer</span>(<span class="params"></span>): <span class="title">never</span> </span>&#123;</span><br><span class="line">  <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>();</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="number">123</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 参数是对象时的注解 （也可以用接口）</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params">&#123; one, two &#125;: &#123; one: <span class="built_in">number</span>; two: <span class="built_in">number</span> &#125;</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> one + two;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> total = add(&#123; <span class="attr">one</span>: <span class="number">1</span>, <span class="attr">two</span>: <span class="number">2</span> &#125;);</span><br></pre></td></tr></table></figure>

<h3 id="1-5-数组的类型注解"><a href="#1-5-数组的类型注解" class="headerlink" title="1.5 数组的类型注解"></a>1.5 数组的类型注解</h3><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> numberArr: <span class="built_in">number</span>[] = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"><span class="keyword">const</span> stringArr: <span class="built_in">string</span>[] = [<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>];</span><br><span class="line"><span class="keyword">const</span> undefinedArr: <span class="literal">undefined</span>[] = [<span class="literal">undefined</span>, <span class="literal">undefined</span>];</span><br><span class="line"><span class="keyword">const</span> arr: (<span class="built_in">number</span> | <span class="built_in">string</span> | <span class="built_in">boolean</span>)[] = [<span class="number">1</span>, <span class="string">&quot;a&quot;</span>, <span class="literal">true</span>, <span class="literal">false</span>]; <span class="comment">// 用到联合类型</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 类型别名 type alias</span></span><br><span class="line"><span class="keyword">type</span> lady = &#123; <span class="attr">name</span>: <span class="built_in">string</span>; age: <span class="built_in">number</span> &#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> xiaojj: lady[] = [</span><br><span class="line">  &#123;</span><br><span class="line">    name: <span class="string">&quot;xiaojj&quot;</span>,</span><br><span class="line">    age: <span class="number">90</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    name: <span class="string">&quot;j&quot;</span>,</span><br><span class="line">    age: <span class="number">30</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line"><span class="comment">// 或用类的方式</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Ady2</span> </span>&#123;</span><br><span class="line">  name: <span class="built_in">string</span>;</span><br><span class="line">  age: <span class="built_in">number</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> xiaojj2: Ady2[] = [</span><br><span class="line">  &#123;</span><br><span class="line">    name: <span class="string">&quot;xiaojj&quot;</span>,</span><br><span class="line">    age: <span class="number">90</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    name: <span class="string">&quot;j&quot;</span>,</span><br><span class="line">    age: <span class="number">30</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">];</span><br></pre></td></tr></table></figure>

<h2 id="2-元组"><a href="#2-元组" class="headerlink" title="2.元组"></a>2.元组</h2><p>元组，可以理解为：<strong>已知元素数量和类型的数组</strong></p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 联合类型</span></span><br><span class="line"><span class="keyword">const</span> xjj: (<span class="built_in">string</span> | <span class="built_in">number</span>)[] = [<span class="string">&quot;a&quot;</span>, <span class="number">22</span>, <span class="string">&quot;b&quot;</span>]; <span class="comment">// 规定整个数组当中可以有string或number</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 元组注解 注意这里的注解只有一个中括号</span></span><br><span class="line"><span class="keyword">const</span> xjj1: [<span class="built_in">string</span>, <span class="built_in">number</span>, <span class="built_in">number</span>] = [<span class="string">&quot;a&quot;</span>, <span class="number">22</span>, <span class="number">33</span>]; <span class="comment">// 规定了数组每个元素对应位置的类型</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// Note: 在开发中元祖的使用在相对少</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// CSV的方式定义数据格式； （二维数组时需要多加一个中括号）</span></span><br><span class="line"><span class="keyword">const</span> xjj2: [<span class="built_in">string</span>, <span class="built_in">number</span>, <span class="built_in">number</span>][] = [</span><br><span class="line">  [<span class="string">&quot;a&quot;</span>, <span class="number">22</span>, <span class="number">33</span>],</span><br><span class="line">  [<span class="string">&quot;a&quot;</span>, <span class="number">22</span>, <span class="number">33</span>],</span><br><span class="line">];</span><br></pre></td></tr></table></figure>

<h2 id="3-接口"><a href="#3-接口" class="headerlink" title="3.接口"></a>3.接口</h2><p>接口，可以理解为对象属性的类型描述。和<code>类型别名</code>类似，不同的是 接口必须是一个对象，而别名可以直接是一个类型，如<code>type Girl = string</code></p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">interface</span> Girl &#123;</span><br><span class="line">  <span class="comment">// 接口 （理解：对象属性的类型描述）</span></span><br><span class="line">  <span class="keyword">readonly</span> name: <span class="built_in">string</span>; <span class="comment">// 只读属性 （定义之后不能再修改）</span></span><br><span class="line">  age: <span class="built_in">number</span>;</span><br><span class="line">  waistline?: <span class="built_in">number</span>; <span class="comment">// 加个问号表示是可选值</span></span><br><span class="line">  [propname: <span class="built_in">string</span>]: <span class="built_in">any</span>; <span class="comment">// 表示可以有不限制属性名的属性，但属性名需要是字符串，值可以是任意类型</span></span><br><span class="line">  say(): <span class="built_in">string</span>; <span class="comment">// 函数类型，返回值是string (如无返回值时是 void)。  say()也可以加入参数类型检查，如say(p:number)</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 和类型别名类似，不同的是 接口必须是一个对象，而别名可以直接是一个类型，如 type Girl = string</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 接口的继承</span></span><br><span class="line"><span class="keyword">interface</span> Teacher <span class="keyword">extends</span> Girl &#123;</span><br><span class="line">  teach(): <span class="built_in">string</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> girl = &#123;</span><br><span class="line">  name: <span class="string">&quot;大脚&quot;</span>,</span><br><span class="line">  age: <span class="number">18</span>,</span><br><span class="line">  sex: <span class="string">&quot;女&quot;</span>,</span><br><span class="line">  <span class="function"><span class="title">say</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;欢迎光临&quot;</span>;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="function"><span class="title">teach</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;教&quot;</span>;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> screenResume = <span class="function">(<span class="params">&#123; name, age, bust, sex &#125;: Girl</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(name, age, bust, sex);</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">const</span> getResume = <span class="function">(<span class="params">&#123; name, age, bust, teach &#125;: Teacher</span>) =&gt;</span> &#123;</span><br><span class="line">  teach();</span><br><span class="line">  <span class="built_in">console</span>.log(name, age, bust);</span><br><span class="line">&#125;;</span><br><span class="line">screenResume(girl);</span><br><span class="line">getResume(girl);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 接口在class类中的使用</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">xiaojjj</span> <span class="title">implements</span> <span class="title">Girl</span> </span>&#123;</span><br><span class="line">  name = <span class="string">&quot;xiaojj&quot;</span>;</span><br><span class="line">  age = <span class="number">18</span>;</span><br><span class="line">  bust = <span class="number">98</span>;</span><br><span class="line">  sex = <span class="string">&quot;女&quot;</span>;</span><br><span class="line">  <span class="function"><span class="title">say</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;欢迎光临&quot;</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>可索引的类型</strong></p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">interface</span> Arr &#123;</span><br><span class="line">  [index: <span class="built_in">number</span>]: <span class="built_in">string</span>; <span class="comment">// 表示通过索引访问数据时返回的类型是string</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> myArr: Arr = [<span class="string">&quot;1&quot;</span>, <span class="string">&quot;2&quot;</span>, <span class="string">&quot;3&quot;</span>];</span><br><span class="line"><span class="keyword">const</span> myStr: <span class="built_in">string</span> = myArr[<span class="number">1</span>];</span><br></pre></td></tr></table></figure>

<h2 id="4-ES6-class-类中应用-TS"><a href="#4-ES6-class-类中应用-TS" class="headerlink" title="4.ES6 class 类中应用 TS"></a>4.ES6 class 类中应用 TS</h2><h3 id="4-1-class-类的修饰器"><a href="#4-1-class-类的修饰器" class="headerlink" title="4.1 class 类的修饰器"></a>4.1 class 类的修饰器</h3><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 类的修饰器：</span></span><br><span class="line"><span class="comment">// public (公共) 允许内外部使用</span></span><br><span class="line"><span class="comment">// protected (受保护的) 允许在内部和继承内使用</span></span><br><span class="line"><span class="comment">// private (私有) 允许在内部使用，但不能在继承中使用</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 类的内部和外部，&#123;&#125;内属于内部，外面是外部</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">  <span class="comment">// public 内外都可以使用，可省略，不写时默认public。 protected只允许内部使用</span></span><br><span class="line">  name: <span class="built_in">string</span>; <span class="comment">// 这里的string注释是TS使用的</span></span><br><span class="line">  <span class="keyword">private</span> age: <span class="number">18</span>;</span><br><span class="line">  <span class="keyword">public</span> <span class="function"><span class="title">sayHello</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">this</span>.name + <span class="built_in">this</span>.age + <span class="string">&quot;say hello&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Teacher2</span> <span class="keyword">extends</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">  <span class="keyword">public</span> <span class="function"><span class="title">sayBye</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">this</span>.name + <span class="string">&quot; say bye&quot;</span>); <span class="comment">// protected 可以在继承中使用到</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> person = <span class="keyword">new</span> Person();</span><br><span class="line">person.name = <span class="string">&quot;test&quot;</span>; <span class="comment">// 在类的外部定义</span></span><br><span class="line"><span class="built_in">console</span>.log(person.name);</span><br></pre></td></tr></table></figure>

<h3 id="4-2-class-类的构造函数中使用类型校验"><a href="#4-2-class-类的构造函数中使用类型校验" class="headerlink" title="4.2 class 类的构造函数中使用类型校验"></a>4.2 class 类的构造函数中使用类型校验</h3><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person2</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params"><span class="keyword">public</span> name: <span class="built_in">string</span></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">this</span>.name = name;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Teacher3</span> <span class="keyword">extends</span> <span class="title">Person2</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params"><span class="keyword">public</span> age: <span class="built_in">number</span></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">super</span>(<span class="string">&quot;test-name&quot;</span>); <span class="comment">// 这里传的值是给Person2的构造函数。即使父类没有构造函数，子类的构造函数内也要写super()</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> teacher3 = <span class="keyword">new</span> Teacher3(<span class="number">18</span>);</span><br><span class="line"><span class="built_in">console</span>.log(teacher3.name);</span><br><span class="line"><span class="built_in">console</span>.log(teacher3.age);</span><br></pre></td></tr></table></figure>

<h3 id="4-3-class-类的-getter、setter-和-static"><a href="#4-3-class-类的-getter、setter-和-static" class="headerlink" title="4.3 class 类的 getter、setter 和 static"></a>4.3 class 类的 getter、setter 和 static</h3><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Xjj</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params"><span class="keyword">private</span> _age: <span class="built_in">number</span></span>)</span> &#123;&#125;</span><br><span class="line">  <span class="keyword">get</span> <span class="title">age</span>() &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>._age - <span class="number">10</span>;</span><br><span class="line">  &#125; <span class="comment">// 访问器属性，以属性的形式访问age，并可以对属性进行包装</span></span><br><span class="line">  <span class="keyword">set</span> <span class="title">age</span>(<span class="params">age: <span class="built_in">number</span></span>) &#123;</span><br><span class="line">    <span class="built_in">this</span>._age = age + <span class="number">3</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> dj = <span class="keyword">new</span> Xjj(<span class="number">28</span>);</span><br><span class="line">dj.age = <span class="number">25</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(dj.age);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 静态属性 static。  静态属性指不需要通过实例化，直接通过Girl.prop的方式就可以访问到属性</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Girl</span> </span>&#123;</span><br><span class="line">  <span class="keyword">static</span> <span class="function"><span class="title">sayLove</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;I love you&quot;</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// const girl = new Girl()</span></span><br><span class="line"><span class="built_in">console</span>.log(Girl.sayLove());</span><br></pre></td></tr></table></figure>

<h3 id="4-4-class-类的只读属性"><a href="#4-4-class-类的只读属性" class="headerlink" title="4.4 class 类的只读属性"></a>4.4 class 类的只读属性</h3><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 只读属性</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">readonly</span> _name: <span class="built_in">string</span>; <span class="comment">// 只读属性</span></span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">name: <span class="built_in">string</span></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">this</span>._name = name;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> person = <span class="keyword">new</span> Person(<span class="string">&quot;testName&quot;</span>);</span><br><span class="line"><span class="comment">// person._name = &#x27;222&#x27;; // 不能修改只读属性</span></span><br><span class="line"><span class="built_in">console</span>.log(person._name);</span><br></pre></td></tr></table></figure>

<h3 id="4-5-抽象类"><a href="#4-5-抽象类" class="headerlink" title="4.5 抽象类"></a>4.5 抽象类</h3><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">/ 抽象类</span><br><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">Girls</span> </span>&#123;</span><br><span class="line">  <span class="keyword">abstract</span> skill(); <span class="comment">// 注意这里只是定义抽象方法，而不具有方法的实现</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Waiter</span> <span class="keyword">extends</span> <span class="title">Girls</span></span>&#123; <span class="comment">// 继承了抽象类之后要 实现抽象类内的成员</span></span><br><span class="line">  <span class="function"><span class="title">skill</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;大爷1&#x27;</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BaseTeacher</span> <span class="keyword">extends</span> <span class="title">Girls</span></span>&#123;</span><br><span class="line">  <span class="function"><span class="title">skill</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;大爷2&#x27;</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SeniorTeacher</span> <span class="keyword">extends</span> <span class="title">Girls</span></span>&#123;</span><br><span class="line">  <span class="function"><span class="title">skill</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;大爷3&#x27;</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="5-联合类型和类型保护"><a href="#5-联合类型和类型保护" class="headerlink" title="5.联合类型和类型保护"></a>5.联合类型和类型保护</h2><p><strong>联合类型</strong> 指某个参数可以是多种类型。</p>
<p><strong>类型保护</strong> 指参数属于某个类型才有相应的操作。</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">interface</span> Waiter &#123;</span><br><span class="line">  anjiao: <span class="built_in">boolean</span>;</span><br><span class="line">  say: <span class="function">() =&gt;</span> &#123;&#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">interface</span> Teacher &#123;</span><br><span class="line">  anjiao: <span class="built_in">boolean</span>;</span><br><span class="line">  skill: <span class="function">() =&gt;</span> &#123;&#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">judgeWho</span>(<span class="params">animal: Waiter | Teacher</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 联合类型</span></span><br><span class="line">  <span class="comment">// 第一种断言方法</span></span><br><span class="line">  <span class="keyword">if</span> (animal.anjiao) &#123;</span><br><span class="line">    <span class="comment">// (animal as Teacher) 的意思是：断言 animal 是 Teacher类型</span></span><br><span class="line">    (animal <span class="keyword">as</span> Teacher).skill();</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    (animal <span class="keyword">as</span> Waiter).say();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 第二种断言方法</span></span><br><span class="line">  <span class="keyword">if</span> (<span class="string">&quot;skill&quot;</span> <span class="keyword">in</span> animal) &#123;</span><br><span class="line">    animal.skill();</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    animal.say();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 第三种类型保护方法是使用typeof来判断 (代码省略)</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">NumberObj</span> </span>&#123;</span><br><span class="line">  count: <span class="built_in">number</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">addObj</span>(<span class="params">first: <span class="built_in">object</span> | NumberObj, second: <span class="built_in">object</span> | NumberObj</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 联合类型</span></span><br><span class="line">  <span class="keyword">if</span> (first <span class="keyword">instanceof</span> NumberObj &amp;&amp; second <span class="keyword">instanceof</span> NumberObj) &#123;</span><br><span class="line">    <span class="comment">// 类型保护</span></span><br><span class="line">    <span class="keyword">return</span> first.count + second.count;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="6-枚举"><a href="#6-枚举" class="headerlink" title="6.枚举"></a>6.枚举</h2><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Enum枚举类型   (个人理解枚举：约定一组可选的常量。 使用常量名表示某个值的含义，增强可读性。)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// js写法</span></span><br><span class="line"><span class="comment">// const Status = &#123;</span></span><br><span class="line"><span class="comment">//   MASSAGE: 0,</span></span><br><span class="line"><span class="comment">//   SPA: 1,</span></span><br><span class="line"><span class="comment">//   DABAOJIAN: 2</span></span><br><span class="line"><span class="comment">// &#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// ts写法</span></span><br><span class="line"><span class="built_in">enum</span> Status &#123;</span><br><span class="line">  MASSAGE, <span class="comment">// 如果想从1开始，给MASSAGE = 1, 即可</span></span><br><span class="line">  SPA,</span><br><span class="line">  DABAOJIAN,</span><br><span class="line">&#125; <span class="comment">// 默认赋值 0 、1、2</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(Status.MASSAGE, Status[<span class="number">0</span>]); <span class="comment">// 0, MASSAGE  可以通过下标反查</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getStatus</span>(<span class="params">status: <span class="built_in">any</span></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (status === Status.MASSAGE) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;massage&quot;</span>;</span><br><span class="line">  &#125; <span class="keyword">else</span> <span class="keyword">if</span> (status === Status.SPA) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;spa&quot;</span>;</span><br><span class="line">  &#125; <span class="keyword">else</span> <span class="keyword">if</span> (status === Status.DABAOJIAN) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;dabaojian&quot;</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> result = getStatus(Status.SPA);</span><br><span class="line"><span class="built_in">console</span>.log(result);</span><br></pre></td></tr></table></figure>

<h2 id="7-泛型"><a href="#7-泛型" class="headerlink" title="7.泛型"></a>7.泛型</h2><p>泛型，最简单的理解：泛指的类型。（类似函数中的形参与实参）</p>
<h3 id="7-1-函数中的泛型使用"><a href="#7-1-函数中的泛型使用" class="headerlink" title="7.1 函数中的泛型使用"></a>7.1 函数中的泛型使用</h3><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// function join(first: string | number, second: string | number) &#123;</span></span><br><span class="line"><span class="comment">//   return `$&#123;first&#125;$&#123;second&#125;`</span></span><br><span class="line"><span class="comment">// &#125;</span></span><br><span class="line"><span class="comment">// join(&#x27;jspang&#x27;, 1); // 如果我想第一个参数是字符串，第二个也必须是字符串，这么就用到泛型</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 泛型使用，如同定义形参，在调用时指定类型</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">join</span>&lt;<span class="title">JSPang</span>&gt;(<span class="params">first: JSPang, second: JSPang</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="string">`<span class="subst">$&#123;first&#125;</span><span class="subst">$&#123;second&#125;</span>`</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">join&lt;<span class="built_in">string</span>&gt;(<span class="string">&quot;jspang&quot;</span>, <span class="string">&quot;123&quot;</span>);</span><br><span class="line">join&lt;<span class="built_in">number</span>&gt;(<span class="number">11</span>, <span class="number">22</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 泛型中数组的使用</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myFun</span>&lt;<span class="title">ANY</span>&gt;(<span class="params">params: ANY[]</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// ANY[] or Array&lt;ANY&gt;</span></span><br><span class="line">  <span class="keyword">return</span> params;</span><br><span class="line">&#125;</span><br><span class="line">myFun&lt;<span class="built_in">string</span>&gt;([<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>]);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 两个类型参数的使用（工作中，常用T表示泛型）</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">join2</span>&lt;<span class="title">T</span>, <span class="title">P</span>&gt;(<span class="params">first: T, second: P</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="string">`<span class="subst">$&#123;first&#125;</span><span class="subst">$&#123;second&#125;</span>`</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">join2&lt;<span class="built_in">string</span>, <span class="built_in">number</span>&gt;(<span class="string">&quot;jspang&quot;</span>, <span class="number">123</span>);</span><br><span class="line">join2&lt;<span class="built_in">number</span>, <span class="built_in">string</span>&gt;(<span class="number">11</span>, <span class="string">&quot;22&quot;</span>);</span><br><span class="line">join2(<span class="number">11</span>, <span class="string">&quot;22&quot;</span>); <span class="comment">// 泛型也支持类型推断 （鼠标移到函数名有提示）</span></span><br></pre></td></tr></table></figure>

<h3 id="7-2-class-类中使用泛型"><a href="#7-2-class-类中使用泛型" class="headerlink" title="7.2 class 类中使用泛型"></a>7.2 class 类中使用泛型</h3><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// class SelectGirl &#123;</span></span><br><span class="line"><span class="comment">//   constructor(private girls: string[] | number[]) &#123; &#125; // private 私有的参数，外部无法修改</span></span><br><span class="line"><span class="comment">//   getGirl(index: number): string | number &#123;</span></span><br><span class="line"><span class="comment">//     return this.girls[index]</span></span><br><span class="line"><span class="comment">//   &#125;</span></span><br><span class="line"><span class="comment">// &#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 使用泛型</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SelectGirl</span>&lt;<span class="title">T</span>&gt; </span>&#123;</span><br><span class="line">  <span class="comment">// 泛型的约束: &lt;T extends number | string&gt;</span></span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params"><span class="keyword">private</span> girls: T[]</span>)</span> &#123;&#125; <span class="comment">// private 私有的参数，外部无法修改</span></span><br><span class="line">  getGirl(index: <span class="built_in">number</span>): T &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.girls[index];</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// const selectGirl = new SelectGirl&lt;string&gt;([&#x27;大脚&#x27;, &#x27;xiaohong&#x27;, &#x27;xiaobai&#x27;])</span></span><br><span class="line"><span class="keyword">const</span> selectGirl = <span class="keyword">new</span> SelectGirl&lt;<span class="built_in">number</span>&gt;([<span class="number">101</span>, <span class="number">102</span>, <span class="number">103</span>]);</span><br><span class="line"><span class="built_in">console</span>.log(selectGirl.getGirl(<span class="number">1</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">// 泛型中的继承</span></span><br><span class="line"><span class="keyword">interface</span> Girl &#123;</span><br><span class="line">  name: <span class="built_in">string</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SelectGirl2</span>&lt;<span class="title">T</span> <span class="keyword">extends</span> <span class="title">Girl</span>&gt; </span>&#123;</span><br><span class="line">  <span class="comment">// 泛型T中必须有一个name属性，继承自Girl接口</span></span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params"><span class="keyword">private</span> girls: T[]</span>)</span> &#123;&#125; <span class="comment">// private 私有的参数，外部无法修改</span></span><br><span class="line">  getGirl(index: <span class="built_in">number</span>): <span class="built_in">string</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.girls[index].name;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> selectGirl2 = <span class="keyword">new</span> SelectGirl2([</span><br><span class="line">  &#123; <span class="attr">name</span>: <span class="string">&quot;大脚1&quot;</span> &#125;,</span><br><span class="line">  &#123; <span class="attr">name</span>: <span class="string">&quot;大脚2&quot;</span> &#125;,</span><br><span class="line">  &#123; <span class="attr">name</span>: <span class="string">&quot;大脚3&quot;</span> &#125;,</span><br><span class="line">]);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(selectGirl2.getGirl(<span class="number">1</span>));</span><br></pre></td></tr></table></figure>

<h2 id="8-配置文件-tsconfig-json"><a href="#8-配置文件-tsconfig-json" class="headerlink" title="8.配置文件 tsconfig.json"></a>8.配置文件 tsconfig.json</h2><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 此文件由命令 tsc -init 生成</span></span><br><span class="line"><span class="comment">// 直接运行 tsc 命令就会运用此配置文件</span></span><br><span class="line"><span class="comment">// 选项详解：https://www.tslang.cn/docs/handbook/compiler-options.html</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="comment">// &quot;include&quot;: [&quot;demo15-1.ts&quot;],              // 要编译的指定文件，不配置此项时运行tsc默认编译全部</span></span><br><span class="line">  <span class="comment">// &quot;files&quot;: [&quot;demo15-1.ts&quot;],                // 和include类似</span></span><br><span class="line">  <span class="comment">// &quot;exclude&quot;: [&quot;demo15-3.ts&quot;],              // 要排除编译的指定文件</span></span><br><span class="line">  <span class="attr">&quot;compilerOptions&quot;</span>: &#123;</span><br><span class="line">    <span class="comment">// 编译选项</span></span><br><span class="line">    <span class="comment">/* 基本选项 */</span></span><br><span class="line">    <span class="comment">// &quot;incremental&quot;: true,                   /* 启用增量编译 */</span></span><br><span class="line">    <span class="attr">&quot;target&quot;</span>: <span class="string">&quot;es5&quot;</span> <span class="comment">/* 指定 ECMAScript 目标版本: &#x27;ES3&#x27; (default), &#x27;ES5&#x27;, &#x27;ES2015&#x27;, &#x27;ES2016&#x27;, &#x27;ES2017&#x27;, &#x27;ES2018&#x27;, &#x27;ES2019&#x27; or &#x27;ESNEXT&#x27;. */</span>,</span><br><span class="line">    <span class="attr">&quot;module&quot;</span>: <span class="string">&quot;commonjs&quot;</span> <span class="comment">/* 指定模块代码生成: &#x27;none&#x27;, &#x27;commonjs&#x27;, &#x27;amd&#x27;, &#x27;system&#x27;, &#x27;umd&#x27;, &#x27;es2015&#x27;, or &#x27;ESNext&#x27;. */</span>,</span><br><span class="line">    <span class="comment">// &quot;lib&quot;: [],                             /* 指定要包含在编译中的库文件. */</span></span><br><span class="line">    <span class="comment">// &quot;allowJs&quot;: true,                       /* 允许编译javascript文件*/</span></span><br><span class="line">    <span class="comment">// &quot;checkJs&quot;: true,                       /* 报告.js文件中的错误。 */</span></span><br><span class="line">    <span class="comment">// &quot;jsx&quot;: &quot;preserve&quot;,                     /* 指定 JSX代码生成: &#x27;preserve&#x27;, &#x27;react-native&#x27;, or &#x27;react&#x27;. */</span></span><br><span class="line">    <span class="comment">// &quot;declaration&quot;: true,                   /* 生成相应的 .d.ts文件 */</span></span><br><span class="line">    <span class="comment">// &quot;declarationMap&quot;: true,                /* 为每个相应的 .d.ts文件生成一个sourcemap (编译后代码对源码的映射)*/</span></span><br><span class="line">    <span class="attr">&quot;sourceMap&quot;</span>: <span class="literal">true</span> <span class="comment">/* 生成源文件与输出文件的映射关系文件(.map)。*/</span>,</span><br><span class="line">    <span class="comment">// &quot;outFile&quot;: &quot;./&quot;,                       /* 将输出文件合并为一个文件. */</span></span><br><span class="line">    <span class="attr">&quot;outDir&quot;</span>: <span class="string">&quot;./build&quot;</span> <span class="comment">/* 输出的js文件目录。 */</span>,</span><br><span class="line">    <span class="attr">&quot;rootDir&quot;</span>: <span class="string">&quot;./src&quot;</span> <span class="comment">/* ts源文件目录。 */</span>,</span><br><span class="line">    <span class="comment">// &quot;composite&quot;: true,                     /* 启用 项目编译 */</span></span><br><span class="line">    <span class="comment">// &quot;tsBuildInfoFile&quot;: &quot;./&quot;,               /* 指定用于存储增量编译信息的文件 */</span></span><br><span class="line">    <span class="comment">// &quot;removeComments&quot;: true,                /* 不输出注释到编译结果.（删除所有注释，除了以 /!*开头的版权信息。） */</span></span><br><span class="line">    <span class="comment">// &quot;noEmit&quot;: true,                        /* 不发出输出.Do not emit outputs. */</span></span><br><span class="line">    <span class="comment">// &quot;importHelpers&quot;: true,                 /* 从“tslib”导入发出助手. Import emit helpers from &#x27;tslib&#x27;. */</span></span><br><span class="line">    <span class="comment">// &quot;downlevelIteration&quot;: true,            /* Provide full support for iterables in &#x27;for-of&#x27;, spread, and destructuring when targeting &#x27;ES5&#x27; or &#x27;ES3&#x27;. */</span></span><br><span class="line">    <span class="comment">// &quot;isolatedModules&quot;: true,               /* 将每个文件作为单独的模块（与“ts.transpileModule”类似） */</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/* 严格的类型检查选项 */</span></span><br><span class="line">    <span class="attr">&quot;strict&quot;</span>: <span class="literal">true</span> <span class="comment">/* 启用所有严格类型检查选项。 打开此选项后，下面这些选项就不需要单独设置*/</span>,</span><br><span class="line">    <span class="comment">// &quot;noImplicitAny&quot;: true,                 /* 对隐含的“any”类型的表达式和声明引发错误. (为false时允许any不用特意声明)*/</span></span><br><span class="line">    <span class="comment">// &quot;strictNullChecks&quot;: true,              /* 启用严格的null检查. (为false时允许赋值为null)*/</span></span><br><span class="line">    <span class="comment">// &quot;strictFunctionTypes&quot;: true,           /* 启用严格检查函数类型. */</span></span><br><span class="line">    <span class="comment">// &quot;strictBindCallApply&quot;: true,           /* 启用函数上严格的“bind”、“call”和“apply”方法. */</span></span><br><span class="line">    <span class="comment">// &quot;strictPropertyInitialization&quot;: true,  /* 启用 严格检查类中的属性初始化. */</span></span><br><span class="line">    <span class="comment">// &quot;noImplicitThis&quot;: true,                /* 在隐含的“any”类型的“this”表达式上引发错误。 */</span></span><br><span class="line">    <span class="comment">// &quot;alwaysStrict&quot;: true,                  /* 以严格模式解析并为每个源文件发出“use strict”。 */</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/* 附加检查。Additional Checks  */</span></span><br><span class="line">    <span class="comment">// &quot;noUnusedLocals&quot;: true,                /* 若有未使用的局部变量则抛错 */</span></span><br><span class="line">    <span class="comment">// &quot;noUnusedParameters&quot;: true,            /* 若有未使用的参数则抛错。 */</span></span><br><span class="line">    <span class="comment">// &quot;noImplicitReturns&quot;: true,             /* 不是函数的所有返回路径都有返回值时报错。*/</span></span><br><span class="line">    <span class="comment">// &quot;noFallthroughCasesInSwitch&quot;: true,    /* 报告switch语句的fallthrough错误。（即，不允许switch的case语句贯穿） */</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/* 模块解析选项 */</span></span><br><span class="line">    <span class="comment">// &quot;moduleResolution&quot;: &quot;node&quot;,            /* 决定如何处理模块：&#x27;node&#x27; (Node.js) or &#x27;classic&#x27; (TypeScript pre-1.6). */</span></span><br><span class="line">    <span class="comment">// &quot;baseUrl&quot;: &quot;./&quot;,                       /* 用于解析非绝对模块名称的基础目录。 */</span></span><br><span class="line">    <span class="comment">// &quot;paths&quot;: &#123;&#125;,                           /* 模块名到基于 baseUrl的路径映射的列表。 */</span></span><br><span class="line">    <span class="comment">// &quot;rootDirs&quot;: [],                        /* List of root folders whose combined content represents the structure of the project at runtime. */</span></span><br><span class="line">    <span class="comment">// &quot;typeRoots&quot;: [],                       /* List of folders to include type definitions from. */</span></span><br><span class="line">    <span class="comment">// &quot;types&quot;: [],                           /* 要包含的类型声明文件名列表 */</span></span><br><span class="line">    <span class="comment">// &quot;allowSyntheticDefaultImports&quot;: true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */</span></span><br><span class="line">    <span class="attr">&quot;esModuleInterop&quot;</span>: <span class="literal">true</span> <span class="comment">/* 启用s emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies &#x27;allowSyntheticDefaultImports&#x27;. */</span></span><br><span class="line">    <span class="comment">// &quot;preserveSymlinks&quot;: true,              /* Do not resolve the real path of symlinks. */</span></span><br><span class="line">    <span class="comment">// &quot;allowUmdGlobalAccess&quot;: true,          /* Allow accessing UMD globals from modules. */</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Source Map Options */</span></span><br><span class="line">    <span class="comment">// &quot;sourceRoot&quot;: &quot;&quot;,                      /* Specify the location where debugger should locate TypeScript files instead of source locations. */</span></span><br><span class="line">    <span class="comment">// &quot;mapRoot&quot;: &quot;&quot;,                         /* Specify the location where debugger should locate map files instead of generated locations. */</span></span><br><span class="line">    <span class="comment">// &quot;inlineSourceMap&quot;: true,               /* Emit a single file with source maps instead of having a separate file. */</span></span><br><span class="line">    <span class="comment">// &quot;inlineSources&quot;: true,                 /* Emit the source alongside the sourcemaps within a single file; requires &#x27;--inlineSourceMap&#x27; or &#x27;--sourceMap&#x27; to be set. */</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Experimental Options */</span></span><br><span class="line">    <span class="comment">// &quot;experimentalDecorators&quot;: true,        /* 启用s experimental support for ES7 decorators. */</span></span><br><span class="line">    <span class="comment">// &quot;emitDecoratorMetadata&quot;: true,         /* 启用s experimental support for emitting type metadata for decorators. */</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
    </div>

    
    
    
      
<div>
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
</div>
        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/JavaScript/" rel="tag"><i class="fa fa-tag"></i> JavaScript</a>
              <a href="/tags/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/" rel="tag"><i class="fa fa-tag"></i> 编程语言</a>
              <a href="/tags/Typescript/" rel="tag"><i class="fa fa-tag"></i> Typescript</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/05/03/%E7%BB%BC%E5%90%88%E9%9D%A2%E8%AF%95%E9%97%AE%E9%A2%98/" rel="prev" title="综合面试问题">
      <i class="fa fa-chevron-left"></i> 综合面试问题
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/05/11/%E8%AF%A6%E8%A7%A3ES6%E4%B8%AD%E7%9A%84class/" rel="next" title="详解ES6中的class">
      详解ES6中的class <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E7%B1%BB%E5%9E%8B%E6%B3%A8%E8%A7%A3"><span class="nav-text">1.类型注解</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E6%9C%89%E5%93%AA%E4%BA%9B%E5%9F%BA%E7%A1%80%E7%B1%BB%E5%9E%8B%E6%B3%A8%E8%A7%A3%EF%BC%9F"><span class="nav-text">1.1 有哪些基础类型注解？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E4%BB%80%E4%B9%88%E6%98%AF%E7%B1%BB%E5%9E%8B%E6%B3%A8%E8%A7%A3%E5%92%8C%E7%B1%BB%E5%9E%8B%E6%8E%A8%E6%96%AD%E3%80%81%E7%B1%BB%E5%9E%8B%E6%96%AD%E8%A8%80%EF%BC%9F"><span class="nav-text">1.2 什么是类型注解和类型推断、类型断言？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E5%AF%B9%E8%B1%A1%E7%9A%84%E7%B1%BB%E5%9E%8B"><span class="nav-text">1.3 对象的类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-%E5%87%BD%E6%95%B0%E7%9A%84%E7%B1%BB%E5%9E%8B%E6%B3%A8%E8%A7%A3"><span class="nav-text">1.4 函数的类型注解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-5-%E6%95%B0%E7%BB%84%E7%9A%84%E7%B1%BB%E5%9E%8B%E6%B3%A8%E8%A7%A3"><span class="nav-text">1.5 数组的类型注解</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%85%83%E7%BB%84"><span class="nav-text">2.元组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%8E%A5%E5%8F%A3"><span class="nav-text">3.接口</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-ES6-class-%E7%B1%BB%E4%B8%AD%E5%BA%94%E7%94%A8-TS"><span class="nav-text">4.ES6 class 类中应用 TS</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-class-%E7%B1%BB%E7%9A%84%E4%BF%AE%E9%A5%B0%E5%99%A8"><span class="nav-text">4.1 class 类的修饰器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-class-%E7%B1%BB%E7%9A%84%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E4%B8%AD%E4%BD%BF%E7%94%A8%E7%B1%BB%E5%9E%8B%E6%A0%A1%E9%AA%8C"><span class="nav-text">4.2 class 类的构造函数中使用类型校验</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-3-class-%E7%B1%BB%E7%9A%84-getter%E3%80%81setter-%E5%92%8C-static"><span class="nav-text">4.3 class 类的 getter、setter 和 static</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-4-class-%E7%B1%BB%E7%9A%84%E5%8F%AA%E8%AF%BB%E5%B1%9E%E6%80%A7"><span class="nav-text">4.4 class 类的只读属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-5-%E6%8A%BD%E8%B1%A1%E7%B1%BB"><span class="nav-text">4.5 抽象类</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E8%81%94%E5%90%88%E7%B1%BB%E5%9E%8B%E5%92%8C%E7%B1%BB%E5%9E%8B%E4%BF%9D%E6%8A%A4"><span class="nav-text">5.联合类型和类型保护</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E6%9E%9A%E4%B8%BE"><span class="nav-text">6.枚举</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-%E6%B3%9B%E5%9E%8B"><span class="nav-text">7.泛型</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#7-1-%E5%87%BD%E6%95%B0%E4%B8%AD%E7%9A%84%E6%B3%9B%E5%9E%8B%E4%BD%BF%E7%94%A8"><span class="nav-text">7.1 函数中的泛型使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-2-class-%E7%B1%BB%E4%B8%AD%E4%BD%BF%E7%94%A8%E6%B3%9B%E5%9E%8B"><span class="nav-text">7.2 class 类中使用泛型</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6-tsconfig-json"><span class="nav-text">8.配置文件 tsconfig.json</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="hxy"
      src="/images/Robben.gif">
  <p class="site-author-name" itemprop="name">hxy</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">80</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">120</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/huxingyi1997" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;huxingyi1997" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:huxingyi1997@zju.edu.cn" title="E-Mail → mailto:huxingyi1997@zju.edu.cn" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-frog"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">hxy</span>
</div>

<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共1039.2k字</span>
</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'pQsO3ySbU4VtWN2j1FLA74Ha-gzGzoHsz',
      appKey     : 'QYacMDY2VY7Wazprg1X6FiUv',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-cn' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

  
  <!-- 动态背景特效 -->
  <!-- 樱花特效 -->
    <script async src="/js/src/sakura.js"></script>
    <script async src="/js/src/fairyDustCursor.js"></script>
</body>
</html>
